package com.yuliang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuliang.constants.SystemConstants;
import com.yuliang.domain.R;
import com.yuliang.domain.dto.ChangeRoleStatusDto;
import com.yuliang.domain.entity.Menu;
import com.yuliang.domain.entity.Role;
import com.yuliang.domain.entity.RoleMenu;
import com.yuliang.domain.vo.*;
import com.yuliang.enums.AppHttpCodeEnum;
import com.yuliang.exception.SystemException;
import com.yuliang.mapper.MenuMapper;
import com.yuliang.mapper.RoleMapper;
import com.yuliang.service.MenuService;
import com.yuliang.service.RoleMenuService;
import com.yuliang.service.RoleService;
import com.yuliang.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2023-10-07 09:50:18
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //管理员
        if (id == 1L){
            List<String> list = new ArrayList<>();
            list.add("admin");
            return list;
        }
        //其他人员
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public R listAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus,SystemConstants.STATUS_NORMAL);
        return R.okResult(list(queryWrapper));
    }

    @Override
    public R getRoleDetail(Long id) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getId,id);
        Role role = getOne(wrapper);
        RoleDetailVo roleDetailVo = BeanCopyUtils.copyBean(role, RoleDetailVo.class);
        return R.okResult(roleDetailVo);
    }

    @Override
    public R pageList(Integer pageNum, Integer pageSize, String roleName, String status) {
        //分页查询
        IPage<Role> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(roleName),Role::getRoleName,roleName);
        queryWrapper.eq(StringUtils.hasText(status),Role::getStatus,status);
        page(page, queryWrapper);
        //vo
        List<RolePageVo> rolePageVos = BeanCopyUtils.copyBeanList(page.getRecords(), RolePageVo.class);
        return R.okResult(new PageVo(rolePageVos,page.getTotal()));
    }

    @Override
    public R changeRoleStatus(ChangeRoleStatusDto changeRoleStatusDto) {
        getBaseMapper().changeRoleStatus(changeRoleStatusDto);
        return R.okResult();
    }

    @Override
    public R addRole(AddRoleVo addRoleVo) {
        try {
            //存role表
            Role role = BeanCopyUtils.copyBean(addRoleVo, Role.class);
            save(role);
            //存role_menu表
            List<RoleMenu> list = new ArrayList<>();
            for (Long menuId:addRoleVo.getMenuIds()) {
                list.add(new RoleMenu(role.getId(),menuId));
            }
            roleMenuService.saveBatch(list);
            return R.okResult();
        } catch (Exception e) {
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
    }

    @Override
    public R updateRole(UpdateRoleVo updateRoleVo) {
        //更新role表
        Role role = BeanCopyUtils.copyBean(updateRoleVo, Role.class);
        updateById(role);
        //更新role_menu表  先删除再存

        List<RoleMenu> list = new ArrayList<>();
        for (Long menuId:updateRoleVo.getMenuIds()) {
            list.add(new RoleMenu(role.getId(),menuId));
        }
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,role.getId());
        roleMenuService.remove(queryWrapper);
        roleMenuService.saveBatch(list);
        return R.okResult();
    }

    @Override
    public R deleteRole(Long id) {
        removeById(id);
        return R.okResult();
    }
}
